<template>
    <AppHeader :title="moduleTypeName" :banAnimate="true" backgroundColor="#ffffff">
      <template #left>
        <img
          @click="onClickLeft"
          src="@/assets/images/icon-back-black.png"
          width="9"
          height="18"
        />
      </template>
      <template #right>
        <img
          @click="onClickRight"
          src="@/assets/images/listening/copy.png"
          width="18"
          height="18"
        />
      </template>
    </AppHeader>
    <div class="container">
      <div class="top">
        <div class="type">选择题</div>
        <div class="num"><span>{{active+1}}</span>/{{datalist.length}}</div>
      </div>
      <div class="swiper-list">
        <div class="list-item" v-for="(item,i) in datalist" :key="i" v-show="i===active">
            <div class="title" v-html="item.stem"/>
            <div v-for="(option,idx) in item.options" :key="idx" class="option"  @click="selectClick(item,idx)">
                <span class="unchecked" :class="[inx === idx ? 'uActive' : '']">{{idx===0?'A':idx===1?'B': idx === 2 ? 'C' : 'D'}}</span>
                <span class="option-item" v-html="option.stem" />
            </div>
        </div>
      </div>
      <div class="btns">
        <div class="btn" @click="prevclick">
          <van-button type="default" block round >上一题</van-button>
        </div>
        <div class="btn" @click="nextclick">
          <van-button type="primary" block round color="linear-gradient( 270deg, #29F19C 0%, #02A1F9 100%)">下一题</van-button>
        </div>
      </div>
    </div>
</template>

<script setup>
import AppHeader from "@/components/AppHeader.vue";
import { useRouter,useRoute } from "vue-router";
import { ref,onMounted ,onBeforeUnmount} from "vue";
const router = useRouter();
const route = useRoute();
const resourceObj = ref({})
const score = ref(0)
var time = 0
const moduleTypeName = ref('')
const onClickLeft = () => {
  router.back();
};
const onClickRight = () => { 
  router.push('/writtenTest/singleSubmit')
}
const datalist = ref([])
onMounted(()=>{
  resourceObj.value = JSON.parse(sessionStorage.getItem('resource'))
  datalist.value = resourceObj.value.parts.find(item => item.sid == route.query.sid).children
  moduleTypeName.value = JSON.parse(sessionStorage.getItem('resource')).parts.find(item=>item.sid == route.query.sid).module_type_name
  if(route.query.num){
    active.value = route.query.num * 1 - 1
  }
})
var timer = setInterval(async ()=>{
  time ++
},1000)
onBeforeUnmount(()=>{
  clearInterval(timer)
  timer = null
})
const active = ref(0);
const inx = ref(null);
const prevclick = () => { 
  //如果是已经选择了，进行回填
  if(active.value > 0){
    active.value --
    var k = datalist.value[active.value].answer ? datalist.value[active.value].answer.sType  : ''
    if(k === 'A'){
      inx.value = 0
    }else if(k === 'B'){
      inx.value = 1
    }else if(k === 'C'){
      inx.value = 2
    }else if(k === 'D'){
      inx.value = 3
    }
    
  }
}
const nextclick = () => {
  if(active.value + 1 < datalist.value.length){
    active.value ++
    var k = datalist.value[active.value].answer ? datalist.value[active.value].answer.sType  : ''
    inx.value= null
    if(k === 'A'){
      inx.value = 0
    }else if(k === 'B'){
      inx.value = 1
    }else if(k === 'C'){
      inx.value = 2
    }else if(k === 'D'){
      inx.value = 3
    }
  }else{
    var num = resourceObj.value.parts.findIndex(item => item.sid == route.query.sid)
    if(num + 1 < resourceObj.value.parts.length){
      router.push({
        path: "/writtenTest/single",
        query: { sid: resourceObj.value.parts[num + 1].sid },
      });
    }else{
      router.push('/writtenTest/singleSubmit')
      sessionStorage.setItem('time',time)
      clearInterval(timer)
    }
  }

}
const selectClick = (item,i) =>{
  inx.value = i
  var sType = ''
  if(i === 0){
    sType = 'A'
  }else if(i === 1){
    sType = 'B'
  }else if(i === 2){
    sType = 'C'
  }else if(i === 3){
    sType = 'D'
  }
  datalist.value[active.value].answer.sorce = 0
  if(item.options[i].id === item.ref_info.answers[0]){
    datalist.value[active.value].answer.sorce = datalist.value[active.value].answer.sorceTotal
  }
  datalist.value[active.value].answer.sType = sType
  datalist.value[active.value].answer.isAnswer = item.options[i].id === item.ref_info.answers[0]
  resourceObj.value.parts.find(item => item.sid == route.query.sid).children =  datalist.value
  sessionStorage.setItem('resource',JSON.stringify(resourceObj.value))
}
</script>

<style lang="scss" scoped>
.container {
  height: 100%;
  background-color: #ffffff;
  padding-top: 44px;
  padding-bottom: 100px;
  overflow-y: auto;
 .top{
  
  padding: 15px 10px;
  display: flex;
  align-items: center;
  justify-content:space-between ;
  .type{
    font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  font-size: 16px;
  color: #333333;
  line-height: 22px;
  text-align: left;
  font-style: normal;
  }
  .num{
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 16px;

    line-height: 22px;
    text-align: center;
    font-style: normal;
    span{
      color: #0CB76C;
    }
  }
 }
 .swiper-list{
  .list-item{
    padding: 0 15px;
    .title{
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 18px;
      color: #333333;
      line-height: 25px;
      text-align: left;
      font-style: normal;
    }
    .option{
      margin: 15px 0;
      .checked{
        background: #0CB76C;
        color: #ffffff;
      }
      .unchecked{
        display:inline-block;
        width: 30px;
        height: 30px;
        border: 1px solid #0CB76C;
        border-radius: 50%;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 16px;
        color: #0CB76C;
        text-align: left;
        font-style: normal;
        text-align: center;
        line-height: 28px;
      }
      .uActive{
        color: #fff;
        background: #0CB76C;
      }
      .option-item{
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        font-size: 18px;
        color: #8E8E8E;
        line-height: 25px;
        text-align: left;
        font-style: normal;
        margin-left: 15px;
      }
    }
  }
 }
 .btns{
  display: flex;
    padding: 30px 10px;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    .btn{
      flex:1;
      &:first-child{
        margin-right: 15px;
      }
    }
 }
}
</style>
